/**
 *
 */
package de.cbf.cam.dsa.facet.garment.editor;

import static de.cbf.cam.dsa.facet.garment.test_support.ConstantsSWTBOTGarment.FACET_GARMENT_BUTTON_EDIT_WEAR_PLACES_ID;
import static de.cbf.cam.dsa.facet.garment.test_support.ConstantsSWTBOTGarment.FACET_GARMENT_COMBO_GENDER_ID;
import static de.cbf.cam.dsa.facet.garment.test_support.ConstantsSWTBOTGarment.FACET_GARMENT_LIST_WEAR_PLACES_ID;
import static de.cbf.cam.dsa.facet.garment.test_support.ConstantsSWTBOTGarment.FACET_GARMENT_TEXT_COLOR_ID;
import static de.cbf.cam.dsa.facet.garment.test_support.ConstantsSWTBOTGarment.FACET_GARMENT_TEXT_LAYER_ID;
import static de.cbf.cam.dsa.facet.garment.test_support.ConstantsSWTBOTGarment.FACET_GARMENT_TEXT_LAYER_SHIFT_INDEX_ID;
import static de.cbf.cam.dsa.facet.garment.test_support.ConstantsSWTBOTGarment.FACET_GARMENT_TEXT_MATERIAL_ID;
import static de.cbf.cam.dsa.facet.garment.test_support.ConstantsSWTBOTGarment.FACET_GARMENT_TEXT_COLD_RESISTANCE_ID;
import static de.cbf.util.ui.test_support.ConstantsSWTBOTUtil.DEFAULT_SWT_BOT_KEY;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

import de.cbf.util.entity.facet.edit.composites.AbstractCompositeEditFacet;
import de.cbf.util.entity.facet.edit.util.FieldDecoratorUtil;
import de.cbf.util.ui.listener.UiChangeEvent;


/**
 * @author Nebelritter
 *
 */
public class CompositeEditGarment extends AbstractCompositeEditFacet {

	public static final int CMD_SELECTED_GENDER			= 1;
	public static final int CMD_EDIT_WEAR_PLACES		= 2;
	public static final int IDX_LAYER_SHIFT_INDEX		= 3;
	public static final int IDX_LAYER					= 4;
	public static final int IDX_MATERIAL				= 5;
	public static final int IDX_COLOR					= 6;
	public static final int IDX_COLD_RESISTANCE 		= 7;

	protected static final String LABEL_GENDER_TEXT						= "Gender: ";
	protected static final String LABEL_COLOR_TEXT						= "Color: ";
	protected static final String LABEL_MATERIAL_TEXT					= "Material: ";
	protected static final String LABEL_LAYER_TEXT						= "Layer: ";
	protected static final String LABEL_LAYER_SHIFT_INDEX_TEXT			= "Layer Shiftindex: ";
	protected static final String LABEL_COLD_RESISTANCE_TEXT 			= "Cold Resistance: ";
	protected static final String LABEL_WEAR_PLACES_TEXT				= "Wear places: ";
	protected static final String BUTTON_EDIT_WEAR_PLACES_TEXT			= "Edit: ";
		
	protected Combo cb_Gender;
	protected Text tx_Color;
	protected Text tx_Material;
	protected Text tx_Layer;
	protected Text tx_LayerShiftIndex;
	protected Text tx_ColdResistance;
	protected org.eclipse.swt.widgets.List li_WearPlaces;
	protected Button bt_EditWearPlaces;
	
	protected ControlDecoration decoration_layer;
	protected ControlDecoration decoration_layer_shift_index;
	protected ControlDecoration decoration_cold_resistance;

	public CompositeEditGarment(Composite parent, int style,FormToolkit toolkit) {
		super(parent, style,toolkit);
	}

	@Override
	protected GridLayout createLayout() {
		return new GridLayout(2, false);
	}
		
	@Override
	protected void createContent(Composite compositeThis) {
		//gender
		Label lb_Gender = toolkit.createLabel(compositeThis, LABEL_GENDER_TEXT,SWT.NONE);
		lb_Gender.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		cb_Gender = new Combo(compositeThis,SWT.READ_ONLY);
		cb_Gender.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,true,1,1));
		cb_Gender.setData(DEFAULT_SWT_BOT_KEY, FACET_GARMENT_COMBO_GENDER_ID);
		toolkit.adapt(cb_Gender,true,true);
		cb_Gender.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				fireUICommand(CMD_SELECTED_GENDER,getSelectedGender());
			}
		});
		
		//fColor
		Label lb_Color = toolkit.createLabel(compositeThis, LABEL_COLOR_TEXT,SWT.NONE);
		lb_Color.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		tx_Color = toolkit.createText(compositeThis, "",SWT.BORDER);
		tx_Color.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		tx_Color.setData(DEFAULT_SWT_BOT_KEY, FACET_GARMENT_TEXT_COLOR_ID);
		tx_Color.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditGarment.this);
				event.setNewValue(tx_Color.getText());
				event.setIndex(IDX_COLOR);
				fireUIChangeEvent(event);
			}
		});		

		//fMaterial
		Label lb_Material = toolkit.createLabel(compositeThis, LABEL_MATERIAL_TEXT,SWT.NONE);
		lb_Material.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		tx_Material = toolkit.createText(compositeThis, "",SWT.BORDER);
		tx_Material.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		tx_Material.setData(DEFAULT_SWT_BOT_KEY, FACET_GARMENT_TEXT_MATERIAL_ID);
		tx_Material.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditGarment.this);
				event.setNewValue(tx_Material.getText());
				event.setIndex(IDX_MATERIAL);
				fireUIChangeEvent(event);
			}
		});		

		//fLayer
		Label lb_Layer = toolkit.createLabel(compositeThis, LABEL_LAYER_TEXT,SWT.NONE);
		lb_Layer.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		tx_Layer = toolkit.createText(compositeThis, "",SWT.BORDER);
		tx_Layer.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		tx_Layer.setData(DEFAULT_SWT_BOT_KEY, FACET_GARMENT_TEXT_LAYER_ID);
		tx_Layer.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditGarment.this);
				event.setNewValue(tx_Layer.getText());
				event.setIndex(IDX_LAYER);
				fireUIChangeEvent(event);
			}
		});	
		// Create the decoration for fLayer
		decoration_layer = new ControlDecoration(tx_Layer,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_layer = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_layer.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_ANY);
		decoration_layer.setImage(image_layer);
		decoration_layer.hide();

		//fLayer shift index
		Label lb_LayerShiftIndex = toolkit.createLabel(compositeThis, LABEL_LAYER_SHIFT_INDEX_TEXT,SWT.NONE);
		lb_LayerShiftIndex.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		tx_LayerShiftIndex = toolkit.createText(compositeThis, "",SWT.BORDER);
		tx_LayerShiftIndex.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		tx_LayerShiftIndex.setData(DEFAULT_SWT_BOT_KEY, FACET_GARMENT_TEXT_LAYER_SHIFT_INDEX_ID);
		tx_LayerShiftIndex.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditGarment.this);
				event.setNewValue(tx_LayerShiftIndex.getText());
				event.setIndex(IDX_LAYER_SHIFT_INDEX);
				fireUIChangeEvent(event);
			}
		});	
		// Create the decoration for fLayer shift index
		decoration_layer_shift_index = new ControlDecoration(tx_LayerShiftIndex,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_layer_shift_index = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_layer_shift_index.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_ANY);
		decoration_layer_shift_index.setImage(image_layer_shift_index);
		decoration_layer_shift_index.hide();

		//fColdResistance
		Label lb_ColdResistance = toolkit.createLabel(compositeThis, LABEL_COLD_RESISTANCE_TEXT,SWT.NONE);
		lb_ColdResistance.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		tx_ColdResistance = toolkit.createText(compositeThis, "",SWT.BORDER);
		tx_ColdResistance.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		tx_ColdResistance.setData(DEFAULT_SWT_BOT_KEY, FACET_GARMENT_TEXT_COLD_RESISTANCE_ID);
		tx_ColdResistance.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditGarment.this);
				event.setNewValue(tx_ColdResistance.getText());
				event.setIndex(IDX_COLD_RESISTANCE);
				fireUIChangeEvent(event);
			}
		});	
		// Create the decoration for fLayer
		decoration_cold_resistance = new ControlDecoration(tx_ColdResistance,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_ColdResistance = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_cold_resistance.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_ANY);
		decoration_cold_resistance.setImage(image_ColdResistance);
		decoration_cold_resistance.hide();
		
		//wear places
		Label lb_WearPlaces = toolkit.createLabel(compositeThis, LABEL_WEAR_PLACES_TEXT,SWT.NONE);
		lb_WearPlaces.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));

		Composite cp_WearPlaces = createWearPlacesSubComposite(compositeThis);
		cp_WearPlaces.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
	}

	protected Composite createWearPlacesSubComposite(Composite compositeThis) {
		Composite cp_WearPlaces = toolkit.createComposite(compositeThis, SWT.NONE);
		//important to have no "inner" margins
		GridLayout glo_WearPlaces = new GridLayout(2, false);
		glo_WearPlaces.marginWidth = 0;
		glo_WearPlaces.marginHeight = 0;
		cp_WearPlaces.setLayout(glo_WearPlaces);

		li_WearPlaces = new org.eclipse.swt.widgets.List(cp_WearPlaces, SWT.READ_ONLY|SWT.BORDER);
		li_WearPlaces.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3));
		li_WearPlaces.setData(DEFAULT_SWT_BOT_KEY, FACET_GARMENT_LIST_WEAR_PLACES_ID);

		bt_EditWearPlaces = toolkit.createButton(cp_WearPlaces, BUTTON_EDIT_WEAR_PLACES_TEXT,SWT.PUSH);
		bt_EditWearPlaces.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		bt_EditWearPlaces.setData(DEFAULT_SWT_BOT_KEY, FACET_GARMENT_BUTTON_EDIT_WEAR_PLACES_ID);
		bt_EditWearPlaces.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				fireUICommand(CMD_EDIT_WEAR_PLACES,(Object[])null);
			}
		});
		return cp_WearPlaces;
	}

	

	public String getSelectedGender() {
		return cb_Gender.getText();
	}

	public void setSelectedGender(String gender) {
		if(cb_Gender.getText().equals(gender)){
			return;
		}
		if(gender == null){
			cb_Gender.deselectAll();
			return;
		}
		cb_Gender.setText(gender);
	}

	public void setPossibleGenders(List<String> possibleGenders) {
		String[] newGenders = possibleGenders.toArray(new String[possibleGenders.size()]);
		if(cb_Gender.getItems().equals(newGenders)){
			return;
		}
		cb_Gender.setItems(newGenders);
		if(newGenders.length > 0){ //select the first if any elements available
			cb_Gender.select(0);
		}
	}

	public String getColor() {
		return tx_Color.getText();
	}

	public void setColor(String newColor) {
		if(tx_Color.getText().equals(newColor)){
			return;
		}
		tx_Color.setText(newColor);
	}

	public String getMaterial() {
		return tx_Material.getText();
	}

	public void setMaterial(String newMaterial) {
		if(tx_Material.getText().equals(newMaterial)){
			return;
		}
		tx_Material.setText(newMaterial);
	}

	public String getLayer() {
		return tx_Layer.getText();
	}

	public void setLayer(String layer) {
		if(tx_Layer.getText().equals(layer)){
			return;
		}
		tx_Layer.setText(layer);
	}

	public String getLayerShiftIndex() {
		return tx_LayerShiftIndex.getText();
	}

	public void setLayerShiftIndex(String layerShiftIndex) {
		if(tx_LayerShiftIndex.getText().equals(layerShiftIndex)){
			return;
		}
		tx_LayerShiftIndex.setText(layerShiftIndex);
	}

	public List<String> getWearPlaces() {
		return Arrays.asList(li_WearPlaces.getItems());
	}

	public void setWearPlaces(List<String> wearPlaces) {
		String[] newWearPlaces = wearPlaces.toArray(new String[wearPlaces.size()]);
		if(li_WearPlaces.getItems().equals(newWearPlaces)){
			return;
		}
		li_WearPlaces.setItems(newWearPlaces);
	}
	
	public String getColdResistance() {
		return tx_ColdResistance.getText();
	}

	public void setColdResistance(String coldResistance) {
		if(tx_ColdResistance.getText().equals(coldResistance)){
			return;
		}
		tx_ColdResistance.setText(coldResistance);		
	}	

	



	public void setDecorationLayerStatus(boolean active){
		if(active){
			decoration_layer.show();
		}else{
			decoration_layer.hide();
		}
	}
	
	public void setDecorationLayerShiftIndexStatus(boolean active){
		if(active){
			decoration_layer_shift_index.show();
		}else{
			decoration_layer_shift_index.hide();
		}
	}
	
	public void setDecorationColdResistanceStatus(boolean active) {
		if(active){
			decoration_cold_resistance.show();
		}else{
			decoration_cold_resistance.hide();
		}		
	}



	
}
